package org.jeecg.modules.sggg.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.sggg.entity.SgItemIntervention;
import org.jeecg.modules.sggg.service.ISgItemInterventionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @Description: 干预方案项目表
 * @Author: jeecg-boot
 * @Date: 2022-10-17
 * @Version: V1.0
 */
@Api(tags = "干预方案项目表")
@RestController
@RequestMapping("/sggg/sgItemIntervention")
@Slf4j
public class SgItemInterventionController extends JeecgController<SgItemIntervention, ISgItemInterventionService> {

    @Autowired
    private ISgItemInterventionService sgItemInterventionService;

    @ApiOperation(value = "干预方案项目表-分页列表查询", notes = "干预方案项目表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<SgItemIntervention>> queryPageList(SgItemIntervention sgItemIntervention,
                                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                           HttpServletRequest req) {
        QueryWrapper<SgItemIntervention> queryWrapper = QueryGenerator.initQueryWrapper(sgItemIntervention, req.getParameterMap());
        Page<SgItemIntervention> page = new Page<>(pageNo, pageSize);
        IPage<SgItemIntervention> pageList = sgItemInterventionService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @AutoLog(value = "干预方案项目表-添加")
    @ApiOperation(value = "干预方案项目表-添加", notes = "干预方案项目表-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody SgItemIntervention sgItemIntervention) {
        sgItemInterventionService.save(sgItemIntervention);
        return Result.OK("添加成功！");
    }

    @AutoLog(value = "干预方案项目表-编辑")
    @ApiOperation(value = "干预方案项目表-编辑", notes = "干预方案项目表-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody SgItemIntervention sgItemIntervention) {
        sgItemInterventionService.updateById(sgItemIntervention);
        return Result.OK("编辑成功!");
    }

    @AutoLog(value = "干预方案项目表-通过id删除")
    @ApiOperation(value = "干预方案项目表-通过id删除", notes = "干预方案项目表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id") String id) {
        sgItemInterventionService.removeById(id);
        return Result.OK("删除成功!");
    }

    @AutoLog(value = "干预方案项目表-批量删除")
    @ApiOperation(value = "干预方案项目表-批量删除", notes = "干预方案项目表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids") String ids) {
        this.sgItemInterventionService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    @ApiOperation(value = "干预方案项目表-通过id查询", notes = "干预方案项目表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<SgItemIntervention> queryById(@RequestParam(name = "id") String id) {
        SgItemIntervention sgItemIntervention = sgItemInterventionService.getById(id);
        if (sgItemIntervention == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(sgItemIntervention);
    }

    /**
     * 导出excel
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SgItemIntervention sgItemIntervention) {
        return super.exportXls(request, sgItemIntervention, SgItemIntervention.class, "干预方案项目表");
    }

    /**
     * 通过excel导入数据
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SgItemIntervention.class);
    }

}
